home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / jetset.1 / jetset.tar / score.c < prev    next >
C/C++ Source or Header  |  1994-02-27  |  5KB  |  230 lines

  1. #include <stdio.h>
  2. #include <pwd.h>
  3.  
  4. #include "game.h"
  5. #include "score.h"
  6.  
  7.  
  8. static char *scorefile = SCOREFILE;
  9. static char *lockfile = LOCKFILE;
  10.  
  11. typedef struct {
  12.     char *pseudo;
  13.     char *name;
  14.     int items;
  15.     int lives;
  16.     int time;
  17.     int uid;
  18.     int active;
  19. } Score;
  20.  
  21.  
  22. static Score *scores;
  23. static int max_scores = 10;
  24. static int nb_scores = 0;
  25.  
  26.  
  27. static int lock_scores ()
  28. {
  29. #define TRIES 2
  30.     int i = 0;
  31.     for (i=0; i<TRIES; i++) {
  32.     int res;
  33.     if (i>0)
  34.         sleep (1);
  35.     res = mkdir (lockfile, 0400);
  36.     if (res == 0)
  37.         break;
  38.     }
  39.     if (i == TRIES) {
  40.     fprintf (stderr, "Couldn't lock score file.\n");
  41.     return 0;
  42.     }
  43.     return 1;
  44. }
  45.  
  46.  
  47. static int unlock_scores ()
  48. {
  49.     int res;
  50.     res = rmdir (lockfile);
  51.     if (res == 0)
  52.     return 1;
  53.     fprintf (stderr, "Couldn't unlock score file.\n");
  54.     return 0;
  55. }
  56.  
  57.  
  58. static void clear_scores ()
  59. {
  60.     int i;
  61.  
  62.     if (scores == NULL) {
  63.     scores = (Score *) malloc (max_scores * sizeof (Score));
  64.     }
  65.     for (i=0; i<nb_scores; i++) {
  66.     free (scores[i].pseudo);
  67.     free (scores[i].name);
  68.     }
  69.     nb_scores = 0;
  70. }
  71.  
  72. static void maybe_grow_scores ()
  73. {
  74.     if (nb_scores == max_scores) {
  75.     max_scores *= 2;
  76.     scores = (Score *) realloc (scores,
  77.                     max_scores * sizeof (Score));
  78.     }
  79. }
  80.  
  81. static void load_scores ()
  82. {
  83.     FILE *file;
  84.     char pseudo[100];
  85.     char name[100];
  86.     int items;
  87.     int lives;
  88.     int time;
  89.     int uid;
  90.     int active;
  91.  
  92.     file = fopen (scorefile, "r");
  93.     clear_scores ();
  94.     if (file != NULL) {
  95.     while (1) {
  96.         if (fscanf (file, "%s %s %d %d %d %d %d",
  97.             pseudo, name, &items,
  98.             &lives, &time, &uid, &active) != 7)
  99.         break;
  100.         maybe_grow_scores ();
  101.         scores[nb_scores].pseudo = (char *) malloc (strlen (pseudo) + 1);
  102.         strcpy (scores[nb_scores].pseudo, pseudo);
  103.         scores[nb_scores].name = (char *) malloc (strlen (name) + 1);
  104.         strcpy (scores[nb_scores].name, name);
  105.         scores[nb_scores].items = items;
  106.         scores[nb_scores].lives = lives;
  107.         scores[nb_scores].time = time;
  108.         scores[nb_scores].uid = uid;
  109.         scores[nb_scores].active = active;
  110.         nb_scores++;
  111.     }
  112.     fclose (file);
  113.     }
  114. }
  115.  
  116. static void write_sc (FILE *file,
  117.               char *pseudo, char *name,
  118.               int items, int lives, int time, int uid, int active)
  119. {
  120.     struct passwd *pwd;
  121.     char buf[100];
  122.  
  123.     if (name == NULL) {
  124.     pwd = getpwuid (getuid ());
  125.     if (pwd != NULL)
  126.         name = pwd->pw_name;
  127.     else {
  128.         name = "nobody";
  129.         pseudo = name;
  130.     }
  131.     }
  132.     if (pseudo == NULL) {
  133.     char *p;
  134.     extern char *getenv ();
  135.  
  136.     pseudo = getenv ("NICKNAME");
  137.     if (pseudo == NULL)
  138.         pseudo = getenv ("NAME");
  139.     if (pseudo == NULL)
  140.         pseudo = name;
  141.     strncpy (buf, pseudo, 90);
  142.     buf[90] = '\0';
  143.     pseudo = buf;
  144.     for (p = buf; *p; p++) {
  145.         if (*p == ' ' || *p == '\t' || *p == '\n') {
  146.         *p = '_';
  147.         break;
  148.         }
  149.     }
  150.     }
  151.     fprintf (file, "%s %s %d %d %d %d %d\n",
  152.          pseudo, name, items, lives, time, uid, active);
  153. }
  154.  
  155.  
  156. void save_score (int items, int lives, int time, int flag)
  157. {
  158. #define MAXNB 1000
  159.     FILE *file;
  160.     extern int debugging;
  161.  
  162. /*    if (debugging)
  163.     return;*/
  164.     if (items <= 1 && flag != SCORE_REMOVE)
  165.     return;
  166.     if (!lock_scores ())
  167.     return;
  168.     load_scores ();
  169.     file = fopen (scorefile, "w");
  170.     if (file != NULL) {
  171.     int i;
  172.     int myuid = getuid ();
  173.     int written = (flag == SCORE_REMOVE);
  174.  
  175.     for (i=0; i<((nb_scores<MAXNB)?nb_scores:MAXNB); i++) {
  176.         if (!written) {
  177.         if (items > scores[i].items
  178.             || (items == scores[i].items
  179.             && (lives > scores[i].lives
  180.                 || (lives == scores[i].lives
  181.                 && time < scores[i].time)))) {
  182.             write_sc (file, NULL, NULL, items,
  183.                   lives, time, myuid, flag == SCORE_ACTIVE);
  184.             written = 1;
  185.         }
  186.         }
  187.         if (scores[i].uid != myuid || !scores[i].active)
  188.         write_sc (file,
  189.               scores[i].pseudo, scores[i].name,
  190.               scores[i].items, scores[i].lives,
  191.               scores[i].time, scores[i].uid,
  192.               scores[i].active);
  193.     }
  194.     if (!written)
  195.         write_sc (file, NULL, NULL, items, lives, time, myuid,
  196.               flag == SCORE_ACTIVE);
  197.     fclose (file);
  198.     }
  199.     unlock_scores ();
  200. }
  201.  
  202. void print_scores (int all)
  203. {
  204.     char buf[100];
  205.     char buf2[30];
  206.     int i;
  207.  
  208.     load_scores ();
  209.     printf ("                           items    time\n");
  210.     for (i=0; i<(all?nb_scores:(nb_scores<10?nb_scores:10)); i++) {
  211.     int t = scores[i].time;
  212.     int items = scores[i].items;
  213.  
  214.     sprintf (buf, "(%s)", scores[i].name);
  215.     if (items > WIN_ITEMS)
  216.         sprintf (buf2, "WINNER");
  217.     else if (scores[i].active)
  218.         sprintf (buf2, "(%d live%s)", scores[i].lives,
  219.              (scores[i].lives > 1) ? "s" : "");
  220.     else
  221.         *buf2 = '\0';
  222.     printf ("%-10s %15s  %3d   %02d:%02d:%02d  %s\n",
  223.         buf,
  224.         scores[i].pseudo,
  225.         (items > WIN_ITEMS) ? WIN_ITEMS : items,
  226.         t/3600, (t/60)%60, t%60,
  227.         buf2);
  228.     }
  229. }
  230.